package com.tianji.aigc.tools;

import cn.hutool.core.convert.Convert;
import com.tianji.aigc.config.ToolResultHolder;
import com.tianji.aigc.constants.Constant;
import com.tianji.aigc.tools.result.PrePlaceOrder;
import com.tianji.api.client.trade.TradeClient;
import com.tianji.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @author wys
 * @version V1.0
 * @date 2025-04-23 15:09
 */
@Component
@RequiredArgsConstructor
public class OrderTools {

    private final TradeClient tradeClient;

    @Tool(description = Constant.Tools.PRE_PLACE_ORDER)
    public PrePlaceOrder prePlaceOrder(@ToolParam(description = Constant.ToolParams.COURSE_IDS) List<Number> courseIds,
                                       ToolContext toolContext) {
        UserContext.setUser(Convert.toLong(toolContext.getContext().get(Constant.USER_ID)));
        return Optional.ofNullable(courseIds)
                .map(ids -> PrePlaceOrder.of(tradeClient.prePlaceOrder(ids.stream().map(Number::longValue).toList())))
                .map(v->{
                    String key = String.valueOf(toolContext.getContext().get(Constant.REQUEST_ID));
                    String filed = "prePlaceOrder";
                    ToolResultHolder.put(key, filed, v);
                    return v;
                })
                .orElse(null);
    }
}
